//
//  ZZSettingCell.m
//  NetesaeLottery
//
//  Created by 周英培 on 16/8/5.
//  Copyright © 2016年 周英培. All rights reserved.
//

#import "ZZSettingCell.h"

@implementation ZZSettingCell

+ (instancetype)cellWithTableView:(UITableView *)tableView andItem:(NSDictionary *)item {

    static NSString *ID = @"cell";
    
    ZZSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        cell = [[ZZSettingCell alloc] initWithStyle:[self cellStyleWithItem:item] reuseIdentifier:ID];
        
    }
    
    return cell;
    
}


/// 返回cellStyle样式
+ (UITableViewCellStyle)cellStyleWithItem:(NSDictionary *)item {
    
    NSString *cellStyleStr = item[@"HMCellStyle"];
    
    UITableViewCellStyle style;
    
    if ([cellStyleStr isEqualToString:@"UITableViewCellStyleSubtitle"]) {
        
        style = UITableViewCellStyleSubtitle;
        
    } else if ([cellStyleStr isEqualToString:@"UITableViewCellStyleValue1"]) {
        
        style = UITableViewCellStyleValue1;
        
    } else if ([cellStyleStr isEqualToString:@"UITableViewCellStyleValue2"]) {
        
        style = UITableViewCellStyleValue2;
        
    } else {
        
        style = UITableViewCellStyleDefault;
        
    }
    
    return style;
    
}

- (void)setCellItem:(NSDictionary *)cellItem {

    _cellItem = cellItem;
    
    if ([cellItem[@"HMIcon"] length] > 0) {
        
        self.imageView.image = [UIImage imageNamed:cellItem[@"HMIcon"]];
        
    }
    
    
    self.textLabel.text = cellItem[@"HMTitle"];
    
    self.detailTextLabel.text = cellItem[@"HMSubtitle"];
    
    //MARK:设置子标题的颜色
    if (cellItem[@"HMHighLight"]) {
        
        self.detailTextLabel.textColor = [UIColor redColor];
        
    } else {
    
        self.detailTextLabel.textColor = [UIColor grayColor];
    
    }
    
    //MARK:读取时间数据
    if ([cellItem[@"HMTimeKey"] length] > 0) {
        
      NSString *timeStr = [[NSUserDefaults standardUserDefaults] objectForKey:cellItem[@"HMTimeKey"]];
        
        if (timeStr.length > 0) {
            
            self.detailTextLabel.text = timeStr;
            
        }
        
    }
    
    //MARK:根据字符串，转化为OC类型,设置cell的辅助视图
    NSString *classStr = cellItem[@"HMAccessoryType"];
    
    Class className = NSClassFromString(classStr);
    
    id obj = [[className alloc] init];
    
    if ([obj isKindOfClass:[UIImageView class]]) {
        
        UIImageView *imgView = (UIImageView *)obj;
        
        imgView.image = [UIImage imageNamed:cellItem[@"HMAccessoryName"]];
        
        [imgView sizeToFit];
        
        self.accessoryView = imgView;
        
    } else if ([obj isKindOfClass:[UISwitch class]]) {
        
        UISwitch *switcher = (UISwitch *)obj;
        
        //MARK:给开关设置监听事件，开关状态改变的时候，进行状态改变数据的存储
        [switcher addTarget:self action:@selector(switcherStatusChange:) forControlEvents:UIControlEventValueChanged];
        
        //MARK:即将显示开关的时候，读取开关状态的数据
        BOOL status = [[NSUserDefaults standardUserDefaults] boolForKey:cellItem[@"HMKeyName"]];
        
        switcher.on = status;
        
        self.accessoryView = switcher;
        
    }
}

/// 开关状态改变进行状态数据的存储
- (void)switcherStatusChange:(UISwitch *)sender {

    [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:self.cellItem[@"HMKeyName"]];
    
    [[NSUserDefaults standardUserDefaults] synchronize];

}

- (void)setTimeStr:(NSString *)timeStr {

    _timeStr = timeStr;
    
    self.detailTextLabel.text = timeStr;
    
    //MARK:存储时间数据
    [[NSUserDefaults standardUserDefaults] setObject:timeStr forKey:self.cellItem[@"HMTimeKey"]];
    
    [[NSUserDefaults standardUserDefaults] synchronize];

}

@end
